package MallSecret

import (
	"fmt"
	CoreSQL "gitee.com/fotomxq/weeekj_core/v5/core/sql"
	Router2SystemConfig "gitee.com/fotomxq/weeekj_core/v5/router2/system_config"
)

type ArgsUpdateSecondStatus struct {
	//秒杀商品id
	ID int64 `db:"id" json:"id"`
	//商品id
	ProductID int64 `db:"product_id" json:"productID"`
	//状态
	Status int `db:"status" json:"status"`
}

// UpdateSecondStatus 更新秒杀商品状态
func UpdateSecondStatus(args *ArgsUpdateSecondStatus) (err error) {
	if args.Status == 1 {
		_, err = CoreSQL.UpdateOne(Router2SystemConfig.MainDB.DB, "UPDATE mall_second SET status=:status ,update_at = NOW()  WHERE id=:id ",
			map[string]interface{}{
				"status": args.Status,
				"id":     args.ID,
			})
		//变更结束的同时 delete_at 将未当前时间
	} else if args.Status == 2 {
		_, err = CoreSQL.UpdateOne(Router2SystemConfig.MainDB.DB, "UPDATE mall_second SET status=:status ,update_at = NOW(),delete_at = NOW()  WHERE id=:id ",
			map[string]interface{}{
				"status": args.Status,
				"id":     args.ID,
			})
	}
	return
}

// ArgsUpdateMallBySecond 秒杀商品修改标识参数
type ArgsUpdateMallBySecond struct {
	//ID
	ID int64 `db:"id" json:"id" check:"id"`

	//秒杀状态
	IsSecondMall bool `db:"is_second_mall" json:"IsSecondMall" `
}

// UpdateProductMallBySecond 秒杀商品修改标识
func UpdateProductMallBySecond(args *ArgsUpdateMallBySecond) (err error) {
	_, err = CoreSQL.UpdateOneSoft(Router2SystemConfig.MainDB.DB, "UPDATE mall_core SET update_at = NOW(), is_second_mall = :is_second_mall WHERE id = :id ", args)
	if err != nil {
		return
	}
	//删除缓冲
	deleteProductCache(args.ID)
	//反馈
	return
}
func deleteProductCache(id int64) {
	Router2SystemConfig.MainCache.DeleteMark(getProductCacheMark(id))
}
func getProductCacheMark(id int64) string {
	return fmt.Sprint("mall:core:product:id:", id)
}
